package com.february.todo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    public static final int REQUEST_CODE_REGISTER = 1;
    private Button buttonLogin;
    private EditText etAccount, etPassword;
    private CheckBox cbRemember;

    private String userName = "admin";
    private String pass = "123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //修改标题
        // getSupportActionBar().setTitle("登录");

        initView();
        intData();


        //为登录按钮设置监听事件

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取出输入的内容
                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();
                //判断是否相等

                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(LoginActivity.this, "账号不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.equals(account, userName)) {
                    if (TextUtils.equals(password, pass)) {
                        Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();

                        //用SharedPreferences记住密码
                        if (cbRemember.isChecked()) {
                            SharedPreferences spf = getSharedPreferences("spfRecorid", MODE_PRIVATE);
                            SharedPreferences.Editor editor = spf.edit();
                            editor.putString("account", account);
                            editor.putString("password", password);
                            editor.putBoolean("isRemember", true);
                            editor.apply();
                        } else {
                            SharedPreferences spf = getSharedPreferences("spfRecorid", MODE_PRIVATE);
                            SharedPreferences.Editor editor = spf.edit();
                            editor.putBoolean("isRemember", false);
                            editor.apply();
                        }

                        //传递数据
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra("account", account);
                        startActivity(intent);
                        LoginActivity.this.finish();


                    } else {
                        Toast.makeText(LoginActivity.this, "密码错误！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "用户名错误！", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    //隐藏控件
    private void initView() {
        buttonLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
    }

    //判断是否记住密码
    private void intData() {
        SharedPreferences spf = getSharedPreferences("spfRecorid", MODE_PRIVATE);
        boolean isRemember = spf.getBoolean("isRemember", false);
        String account = spf.getString("account", "");
        String password = spf.getString("password", "");

        userName = account;
        pass = password;

        if (isRemember) {
            etAccount.setText(account);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }
    }

    //去注册界面，并且数据回传
    public void toRegister(View view) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);

        startActivityForResult(intent, REQUEST_CODE_REGISTER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegisterActivity.RESULT_CODE_REGISTER && data != null) {
            Bundle extras = data.getExtras();

            String account = extras.getString("account", "");
            String password = extras.getString("password", "");
            etAccount.setText(account);
            etPassword.setText(password);

            userName = account;
            pass = password;
        }
    }
}